package com.hhc.bookmarkmanager.controller;

import com.hhc.bookmarkmanager.dto.BookmarkDTO;
import com.hhc.bookmarkmanager.service.BookmarkService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/bookmarks")
@RequiredArgsConstructor
@CrossOrigin(origins = "*") // 允许所有跨域请求
public class BookmarkController {

    private final BookmarkService bookmarkService;



    @GetMapping
    public ResponseEntity<List<BookmarkDTO>> getAllBookmarks() {
        return ResponseEntity.ok(bookmarkService.getAllBookmarks());
    }

    @GetMapping("/search")
    public ResponseEntity<List<BookmarkDTO>> searchBookmarks(@RequestParam String query) {
        return ResponseEntity.ok(bookmarkService.searchBookmarks(query));
    }

    @GetMapping("/{id}")
    public ResponseEntity<BookmarkDTO> getBookmarkById(@PathVariable Long id) {
        return ResponseEntity.ok(bookmarkService.getBookmarkById(id));
    }

    @PostMapping
    public ResponseEntity<BookmarkDTO> createBookmark(@RequestBody BookmarkDTO bookmarkDTO) {
        return ResponseEntity.ok(bookmarkService.createBookmark(bookmarkDTO));
    }

    @PutMapping("/{id}")
    public ResponseEntity<BookmarkDTO> updateBookmark(
            @PathVariable Long id, @RequestBody BookmarkDTO bookmarkDTO) {
        return ResponseEntity.ok(bookmarkService.updateBookmark(id, bookmarkDTO));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBookmark(@PathVariable Long id) {
        bookmarkService.deleteBookmark(id);
        return ResponseEntity.noContent().build();
    }
}
